<template>
  <div class="layout-container">
    <!-- 侧边菜单 - 固定定位 -->
    <aside class="sidebar">
      <div class="brand">
        <div class="brand-title">钛考</div>
        <div class="brand-sub">在线考试系统</div>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="menu"
        background-color="#001529"
        text-color="#cdd6e4"
        active-text-color="#ffffff"
        :default-openeds="['system']"
        @select="handleMenuSelect"
      >
        <el-menu-item index="/home">
          <span>主界面</span>
        </el-menu-item>
        <el-sub-menu v-if="userStore.isAdmin || userStore.isTeacher" index="system">
          <template #title>
            <span>系统管理</span>
          </template>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/users">
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin" index="/colleges">
            <span>学院管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin" index="/majors">
            <span>专业管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin" index="/classes">
            <span>班级管理</span>
          </el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="course">
          <template #title>
            <span>课程管理</span>
          </template>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/courses">
            <span>课程管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/questions">
            <span>题库管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/papers">
            <span>试卷管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/exam-management">
            <span>考试管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/grading-list">
            <span>判卷管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/student-exam-records">
            <span>学生考试记录</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isAdmin || userStore.isTeacher" index="/score-statistics">
            <span>成绩统计</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.isStudent" index="/course-selection">
            <span>课程选择</span>
          </el-menu-item>
        </el-sub-menu>
        <el-menu-item v-if="userStore.isStudent" index="/exams">
          <span>在线考试</span>
        </el-menu-item>
        <el-menu-item v-if="userStore.isStudent" index="/my-scores">
          <span>我的成绩</span>
        </el-menu-item>
      </el-menu>
    </aside>

    <!-- 主内容区域 -->
    <div class="main-wrapper">
      <header class="main-header">
        <div class="header-content">
          <div class="header-title">钛考 在线考试系统</div>
          <div class="user-info">
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                {{ userStore.username || '未登录' }}
                <span style="margin-left: 4px">▼</span>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </header>
      <main class="main-content">
        <router-view v-slot="{ Component, route }">
          <transition name="fade" mode="out-in">
            <keep-alive>
              <component :is="Component" :key="route.path" />
            </keep-alive>
          </transition>
        </router-view>
      </main>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'
import { ElMessageBox } from 'element-plus'
import { computed } from 'vue'
const router = useRouter()
const userStore = useUserStore()

const handleCommand = async (command) => {
  if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      
      userStore.logout()
      router.push('/login')
    } catch (error) {
      // 用户取消
    }
  }
}

// 当前激活菜单
const activeMenu = computed(() => router.currentRoute.value.path)

// 菜单选择处理
const handleMenuSelect = (index) => {
  // 忽略子菜单的index（system）
  if (!index || index === 'system' || !index.startsWith('/')) {
    return
  }
  
  const targetPath = index
  const currentPath = router.currentRoute.value.path
  
  // 如果已经在目标页面，不执行跳转
  if (targetPath === currentPath) {
    return
  }
  
  // 执行路由跳转
  router.push(targetPath).catch(err => {
    // 忽略导航重复错误（Vue Router 4 的特性）
    if (err.name !== 'NavigationDuplicated') {
      console.error('路由跳转失败:', err)
    }
  })
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

/* 侧边栏样式 - 固定定位 */
.sidebar {
  background: #001529;
  color: #fff;
  display: block;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100vh;
  width: 220px;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
}

/* 主内容区域包装器 */
.main-wrapper {
  margin-left: 220px;
  width: calc(100% - 220px);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 顶部栏 - 固定定位 */
.main-header {
  position: fixed;
  top: 0;
  right: 0;
  left: 220px;
  z-index: 999;
  background: #409eff;
  color: white;
  line-height: 60px;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  height: 60px;
  flex-shrink: 0;
}

/* 主内容区域 */
.main-content {
  margin-top: 60px;
  padding: 20px;
  background: #f0f2f5;
  width: 100%;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  height: calc(100vh - 60px);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 100%;
}

.header-title { color: #fff; font-size: 16px; letter-spacing: .5px; }

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: white;
  font-size: 14px;
}

/* 路由切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.brand {
  padding: 20px 16px 8px;
}

.brand-title {
  font-size: 20px;
  font-weight: 800;
  color: #fff;
}

.brand-sub {
  font-size: 12px;
  opacity: 0.75;
  color: #fff;
}

.menu { 
  border-right: none; 
  height: calc(100vh - 80px);
  overflow-y: auto;
  overflow-x: hidden;
}
.menu :deep(.el-menu-item) {
  cursor: pointer !important;
  user-select: none;
  pointer-events: auto !important;
}
.menu :deep(.el-menu-item.is-active) {
  background: linear-gradient(90deg, rgba(91,141,239,0.35), rgba(91,141,239,0));
}
.menu :deep(.el-sub-menu__title) {
  cursor: pointer !important;
  user-select: none;
  pointer-events: auto !important;
}
.menu :deep(.el-menu-item:hover) {
  background-color: rgba(255, 255, 255, 0.1) !important;
}
.menu :deep(.el-sub-menu__title:hover) {
  background-color: rgba(255, 255, 255, 0.1) !important;
}
</style>

